import {
  DATE_CUTS,
  FORMAT_DATE,
  INIT_SEARCH_TIME,
  GET_USER_IDS
} from '@/utils/const';
import userStore from '@/store/modules/user';

// 注入组件内数据
const componentData = ref({});
export const injectData = obj => {
  componentData.value = { ...componentData.value, ...obj };
};

// 查询表单
export const searchParams = reactive({
  giveupTime: DATE_CUTS.LAST_31_DAYS()
});

watch(
  () => searchParams.giveupTime,
  val => {
    INIT_SEARCH_TIME(searchParams, val, 'begin_at', 'end_at');
  },
  { immediate: true }
);

// 查询条件
export const formItems = reactive({
  domain: {
    component: 'input',
    label: '域名',
    placeholder: '请输入域名',
    maxlength: 100
  },
  last_use_user_names: {
    component: 'input',
    label: '最后投手',
    placeholder: '请输入最后投手姓名',
    maxlength: 100,
    showOnly: true,
    input() {
      searchParams.last_use_user_ids = GET_USER_IDS(
        searchParams.last_use_user_names
      );
    }
  },
  operate_user_names: {
    component: 'input',
    label: '弃用人员',
    placeholder: '请输入弃用人员姓名',
    maxlength: 100,
    showOnly: true,
    input() {
      searchParams.operate_user_ids = GET_USER_IDS(
        searchParams.operate_user_names
      );
    }
  },
  giveupTime: {
    component: 'date-picker',
    type: 'daterange',
    label: '弃用时间',
    clearable: true,
    showOnly: true,
    shortcuts: [
      { text: '最近7天', value: () => DATE_CUTS.LAST_7_DAYS() },
      { text: '本周', value: () => DATE_CUTS.THIS_WEEK() },
      { text: '本月', value: () => DATE_CUTS.THIS_MONTH() },
      { text: '上周', value: () => DATE_CUTS.PREV_WEEK() },
      { text: '上月', value: () => DATE_CUTS.PREV_MONTH() },
      { text: '最近31天', value: () => DATE_CUTS.LAST_31_DAYS() },
      { text: '最近180天', value: () => DATE_CUTS.LAST_180_DAYS() }
    ]
  }
});

// 表格字段
export const columns = ref([
  { prop: 'domain', label: '域名', fixed: 'left' },
  {
    prop: 'site_type.name',
    label: '站点类型'
  },
  {
    prop: 'company_id',
    label: '所属公司',
    format(data) {
      return userStore().companyMap[data]?.label;
    }
  },
  {
    prop: 'group.name',
    label: '所属团队'
  },
  {
    prop: 'user_id',
    label: '最后投手',
    format(data) {
      return userStore().userMap[data]?.label;
    }
  },
  {
    prop: 'creator_id',
    label: '弃用人员',
    format(data) {
      return userStore().userMap[data]?.label;
    }
  },
  {
    prop: 'created_at',
    label: '弃用时间',
    format(data) {
      return FORMAT_DATE(data);
    }
  },
  {
    label: '操作',
    width: '120px',
    fixed: 'right',
    buttons: [
      {
        label: '详情',
        click({ row }) {
          componentData.value.handleDetail(row);
        }
      }
    ]
  }
]);
